//
//  GMTOurStoreCell.m
//  GMTMall
//  自营订单
//  Created by  Mac on 2020/10/28.
//  Copyright © 2020 lay. All rights reserved.
//

#import "GMTOurStoreCell.h"
#import "OrderGoodsEntityList.h"

@interface GMTOurStoreCell()

@property(nonatomic,strong)UIImageView * headImage;
@property(nonatomic,strong)UIButton * statuBtn;
@property(nonatomic,strong)UILabel * contentLab;
@property(nonatomic,strong)UILabel * payRightLab;
@property(nonatomic,strong)UILabel * sizeLab;
@property(nonatomic,strong)UILabel * multipLab;
@property(nonatomic,strong)UILabel * totalPayBottomLab;
@property(nonatomic,strong)UIButton * lbBtn;
@property(nonatomic,strong)UIButton * rbBtn;
@property(nonatomic,strong)UIButton * afterSaleBtn;

@end

@implementation GMTOurStoreCell

#pragma mark -------- system

- (void)awakeFromNib {
    [super awakeFromNib];
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];
}
//
-(void)setHighlighted:(BOOL)highlighted animated:(BOOL)animated {
}

+ (instancetype)cellWithTableView:(UITableView *)tableView {
    static NSString *identifier = @"GMTOurStoreCell";
    // 1.缓存中取
    GMTOurStoreCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    // 2.创建
    if (cell == nil) {
        cell = [[GMTOurStoreCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
    }
    return cell;
}

/**
 *  构造方法(在初始化对象的时候会调用)
 *  一般在这个方法中添加需要显示的子控件
 */
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        self.selectedBackgroundView = [UIView new];
        
        [self.contentView addSubview:self.headImage];

        [self.contentView addSubview:self.contentLab];

        [self.contentView addSubview:self.payRightLab];

        [self.contentView addSubview:self.sizeLab];

        [self.contentView addSubview:self.multipLab];
        
        [self.contentView addSubview:self.totalPayBottomLab];
        
        [self.contentView addSubview:self.lbBtn];
        
        [self.contentView addSubview:self.rbBtn];
        
        [self.contentView addSubview:self.afterSaleBtn];

    }
    return self;
}

- (void)setFrame:(CGRect)frame {
    frame.origin.x += 10;
    frame.origin.y += 10;
    frame.size.height -= 10;
    frame.size.width -= 20;
    [super setFrame:frame];
}

#pragma mark -------- infoModel


- (void)setModel:(GMTOurOrderModel *)model {
    
    _model = model;
    
    OrderGoodsEntityList * entityModel = model.orderGoodsEntityList.firstObject;
    
    NSString * goodsName = model.goodsName;
    CGRect contentRect = model.contentRect;
    NSInteger  multpl = model.goodsCount;
    NSString * actualPrice = model.actualPrice;
    
    NSString * retailPrice = entityModel.retailPrice;
    NSString * listUrl = [NSString stringWithFormat:@"%@",entityModel.listPicUrl];
    NSString * size = entityModel.skuText;
    

    [self.statuBtn setTitle:_model.title forState:(UIControlStateNormal)];
    
   
    NSString *encodePath = [listUrl stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet characterSetWithCharactersInString:@"`#%^{}\"[]|\\<> "].invertedSet];
    NSURL * url = [NSURL URLWithString:[NSString stringWithFormat:@"%@",encodePath]];
    [self.headImage sd_setImageWithURL:url placeholderImage:[UIImage imageNamed:@""]];
    
//    [self.headImage sd_setImageUrlString:listUrl placeholderImage:nil];
    self.contentLab.frame = contentRect;
    self.contentLab.text = goodsName;
    
    double price = [retailPrice doubleValue];
    self.payRightLab.text = [NSString stringWithFormat:@"￥%.2f",price];
    
    self.multipLab.text = [NSString stringWithFormat:@"x %ld",(long)multpl];
    if([size containsString:@"null"]){
      size =  [size stringByReplacingOccurrencesOfString:@"null" withString:@"规格"];
    }
    
    self.sizeLab.frame = CGRectMake(CGRectGetMinX(contentRect), CGRectGetMaxY(contentRect) + 9, 160, 13);
    self.sizeLab.text = [NSString stringWithFormat:@"%@", @"".appending(size)];
    
    NSString * rightStr = [NSString stringWithFormat:@"￥ %.2f",[actualPrice doubleValue]];
    NSString * leftStr = @"实付： ";
//    NSMutableAttributedString * attr = [NSMutableAttributedString AttributedStringWithString:leftStr.appending(rightStr)];
//    attr.validRange(0, 3).font(UIFontMake(13));
//    attr.validRange(3, attr.length - 3).font(UIFontBoldMake(14));
    self.totalPayBottomLab.text = leftStr.appending(rightStr);
    
    //底部按钮和title
    [self setBottomBtnTittleWithInnerModel];
}

#pragma mark -------- set model inner

-(void)setBottomBtnTittleWithInnerModel {
    self.lbBtn.hidden = YES;
    self.rbBtn.hidden = YES;
    self.afterSaleBtn.hidden = YES;
    for (int i = 0; i < _model.btnsArr.count;  i ++) {
        StoreBtnsModel * model = _model.btnsArr[i];
        UIButton * btn = (UIButton *)[self.contentView viewWithTag:500 + i];
        btn.hidden = NO;
        [btn setTitleColor:model.color forState:(UIControlStateNormal)];
//        [btn setGmt_borderColor:model.color];
        [btn setTitle:model.title forState:(UIControlStateNormal)];
    }
}

#pragma mark -------- action

-(void)btnAction:(UIButton *)button {
    if (self.OurOrderBlock) {
        NSInteger index = button.tag - 500;
        StoreBtnsModel * model = _model.btnsArr[index];
        self.OurOrderBlock(model.clickType);
    }
}

#pragma mark -------- lazy

-(UIImageView *)headImage {
    if (_headImage == nil) {
       _headImage = [[UIImageView alloc] init];
        _headImage.frame = CGRectMake(8, 29, 60, 60);
//        _headImage.backgroundColor = [UIColor groupTableViewBackgroundColor];
        _headImage.layer.borderWidth = 0.5;
        _headImage.layer.cornerRadius = 4;
        _headImage.layer.masksToBounds = YES;
    }
    return _headImage;
}

-(UIButton *)statuBtn {
    if (_statuBtn == nil) {
        _statuBtn = [UIButton buttonWithType:(UIButtonTypeCustom)];
        [self.contentView addSubview:_statuBtn];
        [_statuBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.right.equalTo(self.contentView).offset(-16);
            make.top.equalTo(self.contentView).offset(8);
            make.height.equalTo(@15);
            make.width.mas_greaterThanOrEqualTo(@30);
        }];
        [_statuBtn setTitleColor:GMTBlueColor forState:(UIControlStateNormal)];
        _statuBtn.titleLabel.font = [UIFont systemFontOfSize:13];
    }
    return _statuBtn;
}

-(UILabel *)contentLab {
    if (_contentLab == nil) {
        _contentLab = [[UILabel alloc] init];
        _contentLab.numberOfLines = 0;
        _contentLab.textAlignment = NSTextAlignmentLeft;
        _contentLab.font = UIFontBoldMake(13);
        _contentLab.textColor = GMTDarkGrayColor;
    }
    return _contentLab;
}

-(UILabel *)payRightLab {
    if (_payRightLab == nil) {
        _payRightLab = [[UILabel alloc] init];
        _payRightLab.frame = CGRectMake(SCREEN_WIDTH - 90 - 36, 33, 90, 17);
        _payRightLab.font = [UIFont systemFontOfSize:13];
        _payRightLab.textColor = GMTDarkGrayColor;
        _payRightLab.textAlignment = NSTextAlignmentRight;
    }
    return _payRightLab;
}

-(UILabel *)sizeLab {
    if (_sizeLab == nil) {
        _sizeLab = [[UILabel alloc] init];
        _sizeLab.font = [UIFont systemFontOfSize:12];
        _sizeLab.textColor = [UIColor grayColor];
        _sizeLab.textAlignment = NSTextAlignmentLeft;
        _sizeLab.layer.cornerRadius = 2;
    }
    return _sizeLab;
}

-(UILabel *)multipLab {
    if (_multipLab == nil) {
        _multipLab = [[UILabel alloc] init];
        _multipLab.frame = CGRectMake(SCREEN_WIDTH - 36 - 26, 55, 26, 14);
        _multipLab.font = [UIFont systemFontOfSize:11];
        _multipLab.textColor = [UIColor grayColor];
        _multipLab.textAlignment = NSTextAlignmentRight;
    }
    return _multipLab;
}

-(UILabel *)totalPayBottomLab {
    if (_totalPayBottomLab == nil) {
        _totalPayBottomLab = [[UILabel alloc] init];
        _totalPayBottomLab.textColor = [UIColor blackColor];
        _totalPayBottomLab.frame = CGRectMake(40, 100.5, SCREEN_WIDTH - 40 - 36, 15);
        _totalPayBottomLab.textAlignment = NSTextAlignmentRight;
    }
    return _totalPayBottomLab;
}

-(UIButton *)lbBtn {
    if (_lbBtn == nil) {
        _lbBtn = [UIButton buttonWithType:(UIButtonTypeCustom)];
        _lbBtn.frame = CGRectMake(SCREEN_WIDTH - 84 - 36 - 84 - 10, 123, 84, 32);
        _lbBtn.layer.borderWidth = 1.0;
        _lbBtn.layer.cornerRadius = 2.0;
        _lbBtn.titleLabel.font = [UIFont systemFontOfSize:13];
        _lbBtn.tag = 501;
        [_lbBtn addTarget:self action:@selector(btnAction:) forControlEvents:(UIControlEventTouchUpInside)];
    }
    return _lbBtn;
}

-(UIButton *)rbBtn {
    if (_rbBtn == nil) {
        _rbBtn = [UIButton buttonWithType:(UIButtonTypeCustom)];
        _rbBtn.frame = CGRectMake(SCREEN_WIDTH - 84 - 36, 123, 84, 32);
        _rbBtn.layer.borderWidth = 1.0;
        _rbBtn.layer.cornerRadius = 2.0;
        _rbBtn.titleLabel.font = [UIFont systemFontOfSize:13];
        _rbBtn.tag = 500;
        [_rbBtn addTarget:self action:@selector(btnAction:) forControlEvents:(UIControlEventTouchUpInside)];
    }
    return _rbBtn;
}

-(UIButton *)afterSaleBtn {
    if (_afterSaleBtn == nil) {
        _afterSaleBtn = [UIButton buttonWithType:(UIButtonTypeCustom)];
        _afterSaleBtn.frame = CGRectMake(SCREEN_WIDTH - 84 - 36 - 84 - 10 - 84 - 10 , 123, 84, 32);
        _afterSaleBtn.layer.borderWidth = 1.0;
        _afterSaleBtn.layer.cornerRadius = 2.0;
        _afterSaleBtn.titleLabel.font = [UIFont systemFontOfSize:13];
        _afterSaleBtn.tag = 502;
        [_afterSaleBtn addTarget:self action:@selector(btnAction:) forControlEvents:(UIControlEventTouchUpInside)];
    }
    return _afterSaleBtn;
}

@end

